package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.dshubs.archives.dao.ArchiveSpecialFeatureMapper;
import io.dshubs.archives.dao.ArchiveSpecialFilesMapper;
import io.dshubs.archives.entity.ArchiveSpecialFeature;
import io.dshubs.archives.entity.ArchiveSpecialFiles;
import io.dshubs.archives.service.ArchiveSpecialFilesService;
import io.dshubs.commons.mybatis.enums.DelFlagEnum;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.context.TenantContext;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.enums.SuperTenantEnum;
import io.dshubs.commons.tools.page.PageData;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 专题档案表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-09-03
 */
@Service
public class ArchiveSpecialFilesServiceImpl extends BaseServiceImpl<ArchiveSpecialFilesMapper, ArchiveSpecialFiles> implements ArchiveSpecialFilesService {

    @Autowired
    private ArchiveSpecialFeatureMapper archiveSpecialFeatureMapper;

    @Override
    public PageData<ArchiveSpecialFiles> page(Map<String, Object> params) {
        QueryWrapper<ArchiveSpecialFiles> wrapper = getWrapper(params);
        if(wrapper == null) {

            return null;
        }
        IPage<ArchiveSpecialFiles> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),wrapper

        );
        return getPageData(page, ArchiveSpecialFiles.class);
    }

    private QueryWrapper<ArchiveSpecialFiles> getWrapper(Map<String, Object> params){
        QueryWrapper<ArchiveSpecialFeature> archiveSpecialFeatureQueryWrapper = new QueryWrapper<ArchiveSpecialFeature>();
        String specialFeatureId = (String)params.get("specialFeatureId");
        String specialFeatureName = (String)params.get("specialFeatureName");
        String fileNumber = (String)params.get("fileNumber");
        QueryWrapper<ArchiveSpecialFiles> wrapper = new QueryWrapper<>();
        wrapper.eq(Constant.DEL_FLAG, DelFlagEnum.NORMAL.value());
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
            archiveSpecialFeatureQueryWrapper.eq("user_id",SecurityUser.getUserId());
        } else if (user.getSuperTenant() == SuperTenantEnum.YES.value()) {
            archiveSpecialFeatureQueryWrapper.eq("tenant_code", TenantContext.getTenantCode(user));
        }
        if(StringUtils.isNotEmpty(specialFeatureName)) {
            archiveSpecialFeatureQueryWrapper.like("special_feature_name",specialFeatureName);
        }
        archiveSpecialFeatureQueryWrapper.eq(Constant.DEL_FLAG, DelFlagEnum.NORMAL.value());
        List<ArchiveSpecialFeature> featureList = archiveSpecialFeatureMapper.selectList(archiveSpecialFeatureQueryWrapper);
        if(CollectionUtils.isNotEmpty(featureList)) {
            wrapper.in("special_feature_id",featureList.stream().map(ArchiveSpecialFeature::getId).collect(Collectors.toList()));
        } else {
            return null;
        }

        wrapper.eq(StringUtils.isNotBlank(specialFeatureId), "special_feature_id", specialFeatureId);
        wrapper.like(StringUtils.isNotBlank(fileNumber), "file_number", fileNumber);
        return wrapper;
    }
}
